package com.ray.depot.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ray.depot.service.MaterialLibraryService;
import com.ray.depot.table.entity.MaterialLibrary;
import com.ray.depot.table.entity.MaterialLibraryExample;
import com.ray.depot.table.mapper.MaterialLibraryMapper;
import com.zjhc.flyhong.common.function.enums.DeleteEnum;
import com.zjhc.flyhong.common.function.exception.BussinessException;
import com.zjhc.flyhong.common.function.operator.LoginUser;
import com.zjhc.flyhong.common.function.validate.EntityValidate;
import com.zjhc.flyhong.common.function.validate.rule.FieldRule;
import com.zjhc.flyhong.common.function.validate.rule.RuleEnum;
import com.zjhc.flyhong.common.util.UUIDUtil;
import com.zjhc.flyhong.common.web.ErrorMsg;
import com.zjhc.flyhong.common.web.Result;
import com.zjhc.flyhong.common.web.SuccessMsg;
import com.zjhc.flyhong.common.web.base.BaseService;

@Service
public class MaterialLibraryServiceImpl extends BaseService implements MaterialLibraryService {
	/** 日志对象 **/
	private static Logger logger = Logger.getLogger(MaterialLibraryServiceImpl.class);
   
	@Autowired
	MaterialLibraryMapper  materialLibraryMapper;

	@Override
	public Result<Object> addMaterialLibrary(MaterialLibrary params, LoginUser user) {
		FieldRule fieldRule = new FieldRule();
		fieldRule.addRule(RuleEnum.IS_NOT_NULL, String.class, "libraryId","库位编号")
		.addRule(RuleEnum.IS_NOT_NULL, String.class, "materialId","物料ID")
		.addRule(RuleEnum.IS_NOT_NULL, String.class, "warchouseId","仓库ID");
		Result<Object> rs =	EntityValidate.validate(params, fieldRule);
		if(!rs.codeSuccess())return rs;
		//删除其他关联  一个物料 只能对应一个 仓库的库位
		MaterialLibraryExample example = new MaterialLibraryExample(); 
		example.createCriteria().andWarchouseIdEqualTo(params.getWarchouseId())
		.andMaterialIdEqualTo(params.getMaterialId());
		materialLibraryMapper.deleteByExample(example);
		
		params.setTenantId(user.getTenantId());
		params.setId(UUIDUtil.getUUID());
		params.setIsDelete(DeleteEnum.USE.getValue());
		this.setUserForAdd(params, user);
		int count = materialLibraryMapper.insertSelective(params);
		if (count != 1) {
			logger.error("MaterialLibraryServiceImpl=>addMaterialLibrary is error");
			throw new BussinessException(-1, ErrorMsg.INSERT_ERROR);
		}
		
		return new Result<Object>(0,SuccessMsg.OP_SUCCESS);
	}

	@Override
	public Result<Object> deleteMaterialLibrary(MaterialLibrary params, LoginUser user) {
		/*****************验证开始************************/	
		FieldRule fieldRule = new FieldRule();
		fieldRule.addRule(RuleEnum.IS_NOT_NULL, String.class, "libraryId","库位编号")
		.addRule(RuleEnum.IS_NOT_NULL, String.class, "materialId","物料ID")
		.addRule(RuleEnum.IS_NOT_NULL, String.class, "warchouseId","仓库ID");
		Result<Object> rs =	EntityValidate.validate(params, fieldRule);
		if(!rs.codeSuccess())return rs;
		MaterialLibraryExample example = new MaterialLibraryExample(); 
		example.createCriteria().andLibraryIdEqualTo(params.getLibraryId())
		.andMaterialIdEqualTo(params.getMaterialId());
		materialLibraryMapper.deleteByExample(example);
		return new Result<Object>(0,SuccessMsg.OP_SUCCESS);
	}

	@Override
	public MaterialLibrary selectLibrary(String materialId, String warchouseId) {
		MaterialLibraryExample example = new MaterialLibraryExample(); 
		example.createCriteria().andWarchouseIdEqualTo(warchouseId)
		.andMaterialIdEqualTo(materialId).andIsDeleteEqualTo(DeleteEnum.USE.getValue());
		List<MaterialLibrary> list = materialLibraryMapper.selectByExample(example);
		if(list.isEmpty()){
			return null;
		}
		return list.get(0);
	}

	@Override
	public void deleteMaterialLibraryByLibraryId(String libraryId, LoginUser user) {
		MaterialLibraryExample example = new MaterialLibraryExample(); 
		example.createCriteria().andLibraryIdEqualTo(libraryId);
		materialLibraryMapper.deleteByExample(example);
	}
}
